class Solution {
private:
    unordered_map<int, int> hash_index;
    int end_index;
public:
    TreeNode* helper(int left, int right, const vector<int>& postorder)
    {
        if (left > right) {
            return nullptr;
        }
        int value = postorder[end_index--];
        int index = hash_index[value];
        TreeNode* newnode = new TreeNode(value);
        //先填充右子树
        newnode->right = helper(index + 1, right, postorder);
        newnode->left = helper(left, index - 1, postorder);
        return newnode;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        //先建立哈希表的下标映射
        for (int i = 0;i < inorder.size();++i) {
            hash_index[inorder[i]] = i;
        }
        end_index = inorder.size() - 1;
        TreeNode* res = helper(0, inorder.size() - 1, postorder);
        return res;
    }
};